package com.smarthome.core.housemanager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShDoorService;
import com.smarthome.core.housemanager.HouseDoorplatService;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.bean.ShDoor;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * @author dhc
 * @date 2015年9月14日 上午10:42:23
 */
@Service("houseDoorplatService")
public class HouseDoorplatServiceImpl implements HouseDoorplatService {

	@Resource(name="shDoorService")
	private ShDoorService doorService;
	
	@Override
	public DataGrid getDataGrid(ShDoor door, PageHelper helper) {
		return doorService.queryDataGrid(door, helper);
	}

	@Override
	public ShDoor querydoorById(Long id) {
		return doorService.selectEntryById(id);
	}

	@Override
	public int add(ShDoor door) {
		return doorService.insert(door);
	}

	@Override
	public int edit(ShDoor door) {
		ShDoor saveDoor = doorService.selectEntryById(door.getId());
		if(saveDoor != null){
			BeanUtils.copyProperties(door, saveDoor);
			doorService.saveOrUpdate(door);
		}
		return 0;
	}

	@Override
	public int delte(Long id) {
		return doorService.deleteByIds(id);
	}

	@Override
	public boolean isRepeat(ShDoor door) {
		if(null == door) return false;
		ShDoor queryDoor = new ShDoor();
		queryDoor.setNum(door.getNum());
		queryDoor.setBuildingId(door.getBuildingId());
		List<ShDoor> doors = doorService.selectEntryList(queryDoor);
		if(doors == null || doors.size() == 0){
			return false;
		}
		
		for(ShDoor d : doors){
			if((door.getNum() != null  && d.getNum() == null) ||  door.getNum().equals(d.getNum()) ){
				if(door.getId() != null && d.getId().equals(door.getId())){
					continue;
				}
				return true;
			}
		}
		
		return false;
	}

	/* (non-Javadoc)
	 * @see com.smarthome.core.housemanager.HouseDoorplatService#shShDoorsByBuildUnitId(java.lang.Long)
	 */
	@Override
	public List<ShDoor> shShDoorsByBuildUnitId(Long buildingUnitId) {
		if(null == buildingUnitId) return null;
		ShDoor door = new ShDoor();
		door.setBuildingId(buildingUnitId);
	
		return 	doorService.selectEntryList(door);
	}

	/* (non-Javadoc)
	 * @see com.smarthome.core.housemanager.HouseDoorplatService#getComboboxByUnitId(java.lang.Long)
	 */
	@Override
	public List<Combobox> getComboboxByUnitId(Long unitId) {
		List<Combobox> boxs = new ArrayList<Combobox>();
		List<ShDoor> doors = shShDoorsByBuildUnitId(unitId);
		if(null != doors){
			for(ShDoor d : doors){
				boxs.add(new Combobox(d.getId() + "",d.getNum()));
			}
		}
		
		
		return boxs;
	}

	@Override
	public List<ShDoor> queryDoorByAccount(ShAccount account) {
		return doorService.selectDoorByAccount(account);
	}

}
